﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using TrainingManagement.api.Models.BusinessLogic;
using TrainingManagement.api.Models.BusinessLogic.DepartmentTree;
using TrainingManagement.api.Models.BusinessObjects;

namespace TrainingManagement.api.Controllers
{
    public class DepartmentTreeController : ApiController
    {
        
        public DepartmentTree PutDepartmentTree(DepartmentTree deptTree)
        {
            return new DepartmentTreeCreateOperation().Run(deptTree);
        }

        [System.Web.Http.ActionName("Update")]
        public void PostUpdateDepartmentTree(DepartmentTree deptTree)
        {
            new DepartmentTreeUpdateOperation().Run(deptTree);
        }

        public void DeleteDepartmentTree(int deptTreeId)
        {
            new DepartmentTreeDeleteOperation().Run(deptTreeId);
        }

        public List<DepartmentTree> GetDepartmentTree([FromUri]int? id, [FromUri]string selectBy)
        {
            switch (selectBy)
            {
                case "id":
                    return new DepartmentTreeSelectByIdOperation().Run(id);
                case "parent":
                    if(id == null) throw new Exception("Parent ID cannot be null");
                    return new DepartmentTreeSelectByParentOperation().Run((int)id);
                case "child":
                    if(id == null) throw new Exception("Child ID cannot be null");
                    return new DepartmentTreeSelectByChildOperation().Run((int) id);
                default:
                    throw new Exception(string.Format("{0} is not a valid mode.", selectBy));
            }
        } 
    }
}
